<?php

namespace App\Jobs;

use App\Models\Journal;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Dcat\EasyExcel\Excel;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class JournalImport implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


    protected $result;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    // public function __construct($result)
    // {
    //     //
    //     $this->result = $result;
    // }

    protected $tempPath;

    public function __construct($tempPath)
    {
        //
        $this->tempPath = $tempPath;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $permanentPath = Storage::move($this->tempPath, 'permanent_folder/' . basename($this->tempPath));
        if ($permanentPath) {
            $permanentTempPath = storage_path('app') . DIRECTORY_SEPARATOR . 'permanent_folder/' . basename($this->tempPath);
            // Log::info('====== 临时文件地址 ======' . $permanentTempPath);
            $rows = Excel::import($permanentTempPath)->first()->toArray();
            //
            try {
                // foreach ($this->result as $key => $row) {
                foreach ($rows as $key => $row) {
                    // Log::info('正在写入：' . $key . '===' . '\n' . json_encode($row));
                    $data['name'] = $row['期刊名称'];
                    $data['issn_eissn'] = $row['ISSN / eISSN'];
                    $data['jcr_abbreviation'] = $row['JCR Abbreviation'];
                    $data['type'] = $row['类别/学科'];
                    $data['total_citations'] = $row['Total Citations'];
                    $data['jif_2022'] = $row['2022 JIF'];
                    $data['jif_quartile'] = $row['JIF Quartile'];
                    $data['jci_2022'] = $row['2022 JCI'];
                    $data['oa_gold'] = $row['% of OA Gold'];
                    $data['sjr'] = $row['SJR'];
                    $data['h_index'] = $row['H-Index'];
                    $data['cite_score'] = $row['CiteScore'];
                    $data['snip'] = $row['SNIP'];
                    $data['public_cycle'] = $row['出版周期'];
                    $data['type_label'] = $row['分类标引'];
                    $data['open_access'] = $row['开放获取'];
                    $data['public_difficult'] = $row['发表难度'];
                    $data['publisher'] = $row['出版商'];
                    $data['country_area'] = $row['国家/出版地区'];
                    $data['acceptance_time'] = $row['接收时间'];
                    $data['website'] = $row['官网'];
                    Journal::create($data);
                }
                @unlink($permanentTempPath);
            } catch (\Exception $exception) {
                Log::info('----------catch----------', ['exception' => json_encode($exception)]);
                $this->failed($exception);
            }
        }
    }

    /**
     * 处理任务失败。
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function failed(\Throwable $exception)
    {
        error_log('导入期刊失败, msg: ' . $exception->getMessage());
    }
}
